import request from '../request'
import type { ApiResponse } from '../types'

// Promotion types
export type PromotionType = 'TC' | 'MJ' | 'MS' | 'MZ'

// Base promotion interface
export interface BasePromotion {
  billNo: string
  billCode: string
  promName: string
  promType: PromotionType
  startTime: string
  endTime: string
  status: number
}

// Goods info interface
export interface GoodsInfo {
  id: number
  typeId: number
  title: string
  subTitle: string | null
  mainPic: string | null
  pics: string[] | null
  price: number
  sold: number
  content: string | null
  status: number
  sortNum: number
  attribute: string
  goodsId: string
  entId: string
  goodsCode: string
  oracleGoodsName: string
  goodsSpec: string
  manufacturer: string
  unit: string
  approvalNo: string
  formula: string
  midpack: number
  qty: number
  planCate: string | null
  logogram: string
  wholLimtNum: number
  storNum: number
  isMidpackSaleDisabled: number
  gCategory: string
  qCategory: string
  oracleIsHidden: number
  isPres: number
  saleTaxPriceL1: number
  salePriceL1: number
  saleTaxPriceL2: number
  salePriceL2: number
  saleTaxPriceL3: number
  salePriceL3: number
  saleTaxPriceL4: number
  salePriceL4: number
  brandCode: string
  nextValPrice: number
  createTime: string
  updateTime: string
  nearestExpiredDate?: string
}

// TC (套餐促销) interfaces
export interface ComboDetail {
  id: number
  billSn: number
  goodsId: string
  num: number
  taxprice: number
  status: number
  goodsInfo: GoodsInfo
}

export interface ComboPromotion extends BasePromotion {
  promType: 'TC'
  totalPrice?: number
  details: ComboDetail[]
}

// MJ (满减促销) interfaces
export interface FullReductionDetail {
  id: number
  billSn: number
  taxAmount: number
  billNo: string
  dedRate: number
  status: number
  description: string
}

export interface FullReductionPromotion extends BasePromotion {
  promType: 'MJ'
  details: FullReductionDetail[]
}

// MS (秒杀促销) interfaces
export interface SeckillDetail {
  id: number
  billSn: number
  goodsId: string
  seckillNum: number
  seckillPrice: number
  seckillStorNum: number
  status: number
  goodsInfo: GoodsInfo
}

export interface SeckillPromotion extends BasePromotion {
  promType: 'MS'
  details: SeckillDetail[]
}

// MZ (买赠促销) interfaces
export interface BuyGiftDetail {
  id: number
  billSn: number
  goodsId: string
  num: number
  freeId: string
  freeNum: number
  freeTaxPrice: number
  taxprice: number
  status: number
  goodsInfo: GoodsInfo
  freeGoodsInfo: GoodsInfo
}

export interface BuyGiftPromotion extends BasePromotion {
  promType: 'MZ'
  details: BuyGiftDetail[]
}

// Union type for all promotions
export type Promotion = ComboPromotion | FullReductionPromotion | SeckillPromotion | BuyGiftPromotion

// Promotion Order interfaces
export interface PromoOrderResponse {
  id: number
  orderNum: string
  userId: string
  title: string
  price: number
  status: number
  remark: string
  goodsList: Array<{
    goodsId: string
    price: number
    count: number
    remark: string
    goodsInfo: GoodsInfo
  }>
}

// API functions
export const getPromotionsByType = (promType?: PromotionType) => {
  return request.post<ApiResponse<Promotion[]>>('/app/goods/promo/getComboDetailsByType', {
    promType
  })
}

export const getPromotionsByGoodsId = (goodsId: string) => {
  return request.post<ApiResponse<Promotion[]>>('/app/goods/promo/getCombosByGoodsId', {
    goodsId
  })
}

// Promotion Order APIs
export const createComboOrder = (data: { billNo: string; remark?: string }) => {
  return request.post<ApiResponse<PromoOrderResponse>>('/app/goods/promo/createComboOrder', data)
}

export const createSeckillOrder = (data: { 
  billNo: string
  goodsId: string
  quantity: number
  remark?: string 
}) => {
  return request.post<ApiResponse<PromoOrderResponse>>('/app/goods/promo/createSeckillOrder', data)
}

export const createBuyGiftOrder = (data: { 
  billNo: string
  goodsId: string
  quantity: number
  remark?: string 
}) => {
  return request.post<ApiResponse<PromoOrderResponse>>('/app/goods/promo/createBuyGiftOrder', data)
} 